package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author xiongkunYang
 * @since 2025/05/26 18:30
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Override
    public List<Category> treeSelect(Long parentId) {
        // 根据parentId查找子节点
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, parentId);
        List<Category> categoryList = this.baseMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(categoryList)) {
            categoryList.forEach(category -> {
                // 查找当前节点是否有子节点
                boolean existed = this.baseMapper.exists(new LambdaQueryWrapper<Category>()
                        .eq(Category::getParentId, category.getId()));
                category.setHasChildren(existed);
            });
        }
        return categoryList;
    }

    @Override
    public List<CategoryVo> getLevelOneCategory() {
        // 查询所有一级分类
        List<Category> categoryList = this.baseMapper.selectList(
                new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0)
        );
        // 转换为CategoryVo
        return categoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).toList();
    }

    @Override
    public List<CategoryVo> tree() {
        // 查询所有分类
        List<Category> allCategoryList = this.baseMapper.selectList(null);
        // 转换为CategoryVo
        List<CategoryVo> categoryVoList = allCategoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).toList();
        return buildTree(categoryVoList);
    }

    /**
     * 使用递归方法建分类树
     * @param categoryVoList 所有节点
     * @return 树形结构
     */
    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        //创建树形结构
        List<CategoryVo> trees = new ArrayList<>();
        //遍历所有节点
        for (CategoryVo categoryVo : categoryVoList) {
            //判断是否是根节点
            if (categoryVo.getParentId() == 0) {
                //查找当前节点的子节点，添加到树形结构中
                trees.add(findAndAddChildren(categoryVo, categoryVoList));
            }
        }
        return trees;
    }

    /**
     * 在categoryVoList中找到categoryVo的子节点，并组装到categoryVo中
     * @param categoryVo 父节点
     * @param categoryVoList 所有节点
     * @return 返回组装好的categoryVo节点
     */
    private CategoryVo findAndAddChildren(CategoryVo categoryVo, List<CategoryVo> categoryVoList) {
        //遍历所有节点
        for (CategoryVo node : categoryVoList) {
            //判断是否是categoryVo的子节点
            if(node.getParentId().longValue() == categoryVo.getId().longValue()) {
                //在categoryVoList中找到categoryVo的子节点，
                CategoryVo children = findAndAddChildren(node, categoryVoList);
                //并组装到categoryVo中
                categoryVo.getChildren().add(children);
            }
        }
        return categoryVo;
    }
}
